package com.zh.jbet.core.dao;

import com.zh.jbet.core.entity.Player;
import java.util.List;
import javax.persistence.Query;

public class PlayerDao extends BaseJpaDao<Player>{
    private static final String Q_LOGIN = "SELECT 1 FROM PLAYER p WHERE p.name = ?1 AND p.password = ?2";
    private static final String Q_UPDATE_PWD = "UPDATE PLAYER SET PASSWORD = ?1 WHERE ID = ?2";
    private static final String Q_FIND_BY_NAME = "SELECT object(p) FROM Player p WHERE p.name = ?1";

    public boolean login(String userName, String password) {
        Query q = entityManager.createNativeQuery(Q_LOGIN);
        q.setParameter(1, userName);
        q.setParameter(2, password);
        List r = q.getResultList();
        return !r.isEmpty();
    }

    public void register(Player player, String password) {
        entityManager.persist(player);
        Query q = entityManager.createNativeQuery(Q_UPDATE_PWD);
        q.setParameter(1, password);
        q.setParameter(2, player.getId());
        q.executeUpdate();
    }

    public Player findByName(String name) {
        Query q = entityManager.createQuery(Q_FIND_BY_NAME);
        q.setParameter(1, name);
        List<Player> ret = q.getResultList();
        return ret.isEmpty() ? null : ret.get(0);
    }
}
